package com.singzi.leetcode.N500;

public class T0419 {
    public static void main(String[] args) {
        Solution0419 solution0419 = new Solution0419();
        char[][] board = {
                {'X','.','.','X','.'},
                {'X','.','.','.','X'},
                {'.','.','.','.','.'},
                {'X','.','.','X','X'}
        };
        System.out.println(solution0419.countBattleships(board));
    }
}
class Solution0419 {
    public int countBattleships(char[][] board) {

        int count = 0;
        int m = board.length;
        int n = board[0].length;
//        int [][]visited = new int[m][n];
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if(board[i][j]=='X'){
                    count++;
                    if(j < n-1&&board[i][j+1]=='X'){
                        for(int k = j+1;k < n;k++){
                            if(board[i][k]=='X'){
                                board[i][k]='.';
                            }
                            else break;
                        }
                    }
                    if(i < m-1&&board[i+1][j]=='X'){
                        for(int k = i+1;k < m;k++){
                            if(board[k][j]=='X'){
                                board[k][j]='.';
                            }
                            else break;
                        }
                    }
                }

            }
        }

        return count;

    }
}